package clientService;

import common.Message;
import common.MessageType;
import common.User;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * @author lhn
 * @date 2024-04-30 16:46
 * 用于完成用户的登录和注册等功能
 */

public class UserClientService{

    //方便在其他地方使用User and Socket信息
    private User user = new User();
    private Socket socket;

    //用户登录判断
    public boolean login(String userId, String password) {
        boolean b = false;
        user.setUserId(userId);
        user.setPassword(password);
//        System.out.println(user);
        //连接服务端对象
        try {
            socket = new Socket(InetAddress.getLocalHost(), 9999);
            //ObjectOutputStream对象
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
            //发送user对象
            objectOutputStream.writeObject(user);

            //读取从服务端回复的Message对象
            ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
            Message message = (Message) objectInputStream.readObject();

            //对返回的Message信息进行处理
            if (MessageType.MESSAGE_LOGIN_SUCCEED.equals(message.getMesType())) {
                //TODO 登录成功逻辑
                //创建和服务器保持通信的线程
                ClientConnectServerThread clientConnectServerThread = new ClientConnectServerThread(socket);
                //启动客户端的线程
                clientConnectServerThread.start();
                //将线程放入集合管理
                ManageClientConnectServerThread.addClientConnectServerThread(userId, clientConnectServerThread);
                b = true;
            } else {
                //登录失败关闭socket
                socket.close();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return b;
    }

    /**
    * @author lihaonan
    * @date 2024/5/13 10:19
     * 向服务端请求在线用户列表
    */
    public void onlineFriendList() {
        //包装message
        Message message = new Message();
        message.setSender(user.getUserId());
        message.setMesType(MessageType.MESSAGE_GET_ONLINE_FRIEND);
        //发送给服务端
        //获取到当前线程的Socket对应的 对象输出流
        try {
//            ObjectOutputStream objectOutputStream = new ObjectOutputStream(
//            ManageClientConnectServerThread.getClientConnectServerThread(user.getUserId()).getSocket().getOutputStream()
//            );
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(
                    ManageClientConnectServerThread.getClientConnectServerThread(user.getUserId()).getSocket().getOutputStream()
            );
            objectOutputStream.writeObject(message);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void logout() {
        Message message = new Message();
        message.setSender(user.getUserId());
        message.setMesType(MessageType.MESSAGE_CLIENT_EXIT);

        //发送message
        try {
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(
                    ManageClientConnectServerThread.getClientConnectServerThread(user.getUserId()).getSocket().getOutputStream()
            );
            objectOutputStream.writeObject(message);
            System.out.println("用户" + user.getUserId() + "退出系统");
            System.exit(0); //结束进程
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
